package com.SmartClassroom.system.service.impl;

import com.SmartClassroom.system.common.ErrorCode;
import com.SmartClassroom.system.constant.GradeConstant;
import com.SmartClassroom.system.exception.BusinessException;
import com.SmartClassroom.system.mapper.GradeMapper;
import com.SmartClassroom.system.model.domain.Grade;
import com.SmartClassroom.system.model.dto.grade.GradeQueryRequest;
import com.SmartClassroom.system.model.vo.GradeVO;
import com.SmartClassroom.system.model.vo.LeaveRequestVO;
import com.SmartClassroom.system.service.GradeService;
import com.SmartClassroom.system.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author baijiayu
 * @description 针对表【Grade(成绩)】的数据库操作Service实现
 * @createDate 2024-11-19 11:01:52
 */
@Service
public class GradeServiceImpl extends ServiceImpl<GradeMapper, Grade> implements GradeService {

    @Resource
    private GradeMapper gradeMapper;

    @Resource
    private UserService userService;

    @Override
    public Page<GradeVO> getQueryWrapper(Page<GradeVO> pageParam, GradeQueryRequest gradeQueryRequest) {
        if (gradeQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        // 自定义分页查询
        List<GradeVO> gradeVOList = gradeMapper.getQueryWrapper(pageParam, gradeQueryRequest);
        if (gradeVOList == null) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "查询失败");
        }
        for (GradeVO gradeVO : gradeVOList) {
            switch (gradeVO.getGradeType()) {
                case GradeConstant.FINAL:
                    gradeVO.setGradeType("期末成绩");
                    break;
                case GradeConstant.MIDDLE:
                    gradeVO.setGradeType("期中成绩");
                    break;
                case GradeConstant.PROJECT:
                    gradeVO.setGradeType("实验成绩");
                    break;
                case GradeConstant.TEST:
                    gradeVO.setGradeType("测验成绩");
                    break;
            }
        }
        // 设置分页结果
        pageParam.setRecords(gradeVOList);
        return pageParam;
    }

    @Override
    public Page<GradeVO> getQueryWrapperByMy(Page<GradeVO> pageParam, GradeQueryRequest gradeQueryRequest, String username) {
        if (gradeQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        // 自定义分页查询
        List<GradeVO> gradeVOList = gradeMapper.getQueryByMy(pageParam, gradeQueryRequest, username);
        if (gradeVOList == null) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "查询失败");
        }
        for (GradeVO gradeVO : gradeVOList) {
            switch (gradeVO.getGradeType()) {
                case GradeConstant.FINAL:
                    gradeVO.setGradeType("期末成绩");
                    break;
                case GradeConstant.MIDDLE:
                    gradeVO.setGradeType("期中成绩");
                    break;
                case GradeConstant.PROJECT:
                    gradeVO.setGradeType("实验成绩");
                    break;
                case GradeConstant.TEST:
                    gradeVO.setGradeType("测验成绩");
                    break;
            }
        }
        // 设置分页结果
        pageParam.setRecords(gradeVOList);
        return pageParam;
    }

    @Override
    public List<GradeVO> getQueryWrapperByExecl() {
        // 查询所有数据
        List<GradeVO> gradeList = gradeMapper.getQueryWrapperByExcel();
        if (gradeList == null) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "查询失败");
        }
        for (GradeVO gradeVO : gradeList) {
            switch (gradeVO.getGradeType()) {
                case GradeConstant.FINAL:
                    gradeVO.setGradeType("期末成绩");
                    break;
                case GradeConstant.MIDDLE:
                    gradeVO.setGradeType("期中成绩");
                    break;
                case GradeConstant.PROJECT:
                    gradeVO.setGradeType("实验成绩");
                    break;
                case GradeConstant.TEST:
                    gradeVO.setGradeType("测验成绩");
                    break;
            }
        }
        return gradeList;
    }
}

